package com.wbs.shop.nuby.jsbridge;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sdk.app.PayTask;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wbs.shop.nuby.bean.PayResult;
import com.wbs.shop.nuby.commom.Key;
import com.wbs.shop.nuby.jsbridge.core.BaseInjected;
import com.wbs.shop.nuby.jsbridge.core.JsCallback;
import com.wbs.shop.nuby.utils.Logger;
import com.wbs.shop.nuby.view.listener.IWebView;
import com.wbs.shop.nuby.wxapi.WXPayEntryActivity;

import java.util.Map;

/**
 * Created by Administrator on 2017/3/6 0006.
 */

public class PayInjected extends BaseInjected {
    private JsCallback alipayJsCallback;

    public PayInjected(IWebView webView) {
        super(webView);
    }

    public void wxPay(JSONObject payJsonObject, JsCallback jsCallback) {
        try {
            if (isNotNullWebView()) {
                IWXAPI wxapi = WXAPIFactory.createWXAPI(webViewWeak.get().getContext(), Key.APP_ID);
                PayReq req = new PayReq();
                req.appId = payJsonObject.getString("appid");
                req.partnerId = payJsonObject.getString("partnerid");
                req.prepayId = payJsonObject.getString("prepayid");
                req.nonceStr = payJsonObject.getString("noncestr");
                req.timeStamp = payJsonObject.getString("timestamp");
                req.packageValue = payJsonObject.getString("package");
                req.sign = payJsonObject.getString("sign");
                wxapi.sendReq(req);
                WXPayEntryActivity.setJsCallback(jsCallback);
            }
        } catch (Exception e) {
            Logger.e(e);
        }
    }

    public void aliPay(JSONObject jsonObject, JsCallback jsCallback) {
        try {
            if (isNotNullWebView()) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        PayTask alipay = new PayTask((Activity) webViewWeak.get().getContext());
                        Map<String, String> result = alipay.payV2(jsonObject.getString("data"), true);
                        Message msg = new Message();
                        msg.what = Key.SDK_PAY_FLAG;
                        msg.obj = result;
                        mHandler.sendMessage(msg);
                    }
                }).start();
                alipayJsCallback = jsCallback;
            }
        } catch (Exception e) {
            Logger.e(e);
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Key.SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    if (alipayJsCallback != null) {
                        alipayJsCallback.call(resultStatus);
                        alipayJsCallback = null;
                    }
//                    // 判断resultStatus 为9000则代表支付成功
//                    if (TextUtils.equals(resultStatus, "9000")) {
//                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
//                        //Toast.makeText(TestActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
//                    } else {
//                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
//                        //Toast.makeText(TestActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
//                    }
                    break;
                }
                default:
                    break;
            }
        }
    };
}
